/** Trigger Name: OORTelesalesUpdateHoldTime 
 * Author: Accenture
 * Date: 20-March-2012
 * Requirement/Project Name: OORTelesales
 * Requirement Description: Req 1210
 */
trigger OORTelesalesUpdateHoldTime on Task (after Insert) 
{
    ID oorTelesalesTaskReckType = RecordTypeHelper.GetRecordTypeIdByDevName('Task', OORTelesalesTextConstant.OOR_TELESALES_TASK_REC_TYPE);
    Set<ID> leadIdSet = new set<ID>();
    Set<Id> invalidLeadIdSet = new Set<Id>();
    Set<Id> callDispLeadID= new Set<Id>();     
    List<Lead> leadList = null;
    Integer hoursOnHold =null;
    for(Task taskObj : Trigger.New)
    {
        // process the trigger only for oortelesales task rec type 
        if(taskObj.RecordTypeId == oorTelesalesTaskReckType)
        {
            // check if whoid is null           
            if(taskObj.WhoId !=null  )
            {
                leadIdSet.add(taskObj.WhoID);
            }
            // Check if disposition is of type 'Called left vm' or 'called unavaliable'
            if((taskObj.Disposition__c == OORTelesalesTextConstant.CALLED_LEFT_VM || taskObj.Disposition__c == OORTelesalesTextConstant.CALLED_UNAVALIBLE ) && taskObj.WhoId !=null )
            {
                callDispLeadID.add(taskObj.WhoID);              
            }
            else if ( taskObj.Disposition__c == OORTelesalesTextConstant.INVALID_LOG_DISPOSITION && taskObj.WhoId !=null)
            {   
                invalidLeadIdSet.add(taskObj.WhoID);                
            }
        }
    }
    
    if(leadIdSet.size() > 0)
    {
            leadList = [SELECT Id,NoOfCalls__c , PreviewClock__c,Follow_Up_Date__c,TemporaryOwner__c, Disposition__c,Lead_Status_ReadOnly__c, CampaignTimer__c, OwnerId FROM Lead WHERE Id in:leadIdSet ]; //and PreviewClock__c>0];
    }
     
   try
   { 
        // Check if query has returned any record
        if(leadList!=null && leadList.size() > 0)
        {
            for(Lead leadObj: leadList)
            {   
                // Invalid if user has loged INVALID_LOG_DISPOSITION
                if(invalidLeadIdSet.contains(leadObj.Id))
                {
                    leadObj.OwnerId=OORTelesalesUtility.getQueueID(OORTelesalesTextConstant.OOR_TELE_INVALID_LEAD_QUEUE);
                    leadobj.Lead_Status_ReadOnly__c=OORTelesalesTextConstant.LEAD_INVALID_STATUS; 
                } 
                // Check if user has loged Called left vm' or 'called unavaliable'
                else if(callDispLeadID.contains(leadObj.Id))
                {  
                    hoursOnHold = leadObj.PreviewClock__c !=null ? Integer.valueOf(leadObj.PreviewClock__c):0;      
                    leadObj.CampaignTimer__c = System.now().addHours(hoursOnHold );
                }
                
                
                for(Task taskObj : Trigger.New)
                {
                    if(taskobj.WhoID == leadObj.id && taskobj.Type__c == OORTelesalesTextConstant.CALL_LOG_DISPOSITION_TYPE )
                    {
                        leadObj.LastCallResult__c=taskObj.Disposition__c;
                        leadObj.LastCallDate__c=taskObj.CreatedDate;
                        leadObj.NoOfCalls__c  =  leadObj.NoOfCalls__c == null ? 1 :  leadObj.NoOfCalls__c +1;
                        leadObj.Disposition__c = taskObj.Disposition__c;
                           
                     }
                     else  if(taskobj.WhoID == leadObj.id)
                     {
                         leadObj.Disposition__c = taskObj.Disposition__c;
                     } 
                }
            }
            
            // fire update to the database.
           
            update leadList;     
         }
    }
    catch(Exception e )
    {
        GeneralConstants.mailAnyExceptionToMe('OORTelesalesUpdateHoldTime TRIGGER','OORTelesalesUpdateHoldTime ',e);
    }
}